<template>
    <a-form :model="formParams" ref="formRef" auto-label-width :rules="rules">
		<a-form-item :label="`${currentNode?.isDept ? '部门' : '岗位'}名称:`" field="name">
			<a-input v-model.trim="formParams.name" placeholder="请输入名称" />
		</a-form-item>
		<a-form-item label="上级部门:" v-if="currentNode?.isDept">
			<a-tree-select v-model="formParams.parentId" placeholder="请选择上级部门" :data="treeList" allow-clear allow-search :tree-check-strictly="true" :fieldNames="{key:'id',title:'name'}"></a-tree-select>
		</a-form-item>
        <template v-if="currentNode.isDept">
            <a-form-item label="部门负责人:">
                <a-select v-model="formParams.userId" placeholder="请选择用户">
                    <a-option v-for="item in userList" :key="item.id" :label="item.nickname" :value="item.id" />
                </a-select>
            </a-form-item>
        </template>
		<template v-else>
			<a-form-item label="职务描述:" field="remark">
				<a-textarea v-model.trim="formParams.remark" placeholder="请输入职务描述" />
			</a-form-item>
		</template>
    </a-form>
</template>
<script lang="ts" setup>
import * as Api from '@/api/common';
import mixins from '@/mixins/index.js';
const props = defineProps({
	treeList: { type: [], default: () => { return [] } },
	currentNode: { type: Object, default: () => { return {} } },
})
const { handleMixinSubmit } = mixins();
const emit = defineEmits(['refreshList']);
const formRef = ref(null);
const userList = ref([]); // 用户列表
const formParams = ref({
	name: '', // 部门名称
	parentId:  '', // 上级部门
	remark: '',
	userId: '', // 用户id
});
const rules = ref({
	name: [{ required: true, message: "名称不能为空", trigger: "blur" }],
});
watch(() => props.currentNode, value => {
	console.log('value===', {...value});
	if(value?.id){
		nextTick(async () => {
			await getUserList();
			if(value.btnType === 'edit'){
				formParams.value.name = value.name;
				formParams.value.userId = value.leaderUserIds[0];
				formParams.value.parentId = value.parentId;
			}else{
                if(formRef.value){
                    formRef.value.resetFields();
                }
            }
		})
	}
}, { deep: true, immediate: true })

// 用户列表
const getUserList = async () => {
	try{
		const res = await Api.ApiUserList();
		userList.value = res || [];
	}catch(err){console.log(err);}
}

// 提交
const handleSubmit = async () => {
	const valid = await formRef.value?.validate();
	if (valid) return;
    const { id, btnType, parentId ,isDept } = props.currentNode;
	const { name, remark } = formParams.value
	console.log('props.currentNode====', {...props.currentNode});
	const isEdit = btnType === 'edit';
	console.log('isEdit====', isEdit);
    if(isDept){
	    const params = {
			...formParams.value,
            id: isEdit ? id : null,
            parentId: isEdit ? parentId : id
	    }
	    await handleMixinSubmit(isEdit ? Api.ApiDeptUpdate : Api.ApiDeptAdd, params, '', false);
    }else {
        const params = {
            name,
			remark,
            deptId: id,
	    }
		console.log('params===岗位', {...params});
        await handleMixinSubmit(isEdit ? Api.ApiPostUpdate : Api.ApiPostAdd, params, '', false);
    }
	formParams.value.userId = '';
	emit('refreshList');
}
defineExpose({
	formRef,
    handleSubmit
})
</script>
